//
//  BookSectionPresenter.m
//  iOSCodeLearning
//
//  Created by TFAppleWork-Summer on 2018/1/13.
//  Copyright © 2018年 Ace. All rights reserved.
//

#import "BookSectionPresenter.h"

@interface BookSectionPresenter()

/**
 存储cellPresenter 数组
 */
@property (nonatomic, strong) NSMutableArray  *cellPArray;

/**
 presenter弱引用
 */
@property (nonatomic, weak) UICollectionView *collectionView;

@end

@implementation BookSectionPresenter
@synthesize dataArray = _dataArray;

-(NSInteger)itemCount{
    
    return _dataArray.count;
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    for (CrowdModel *model in dataArray) {
        [self.cellPArray addObject:[[BooksCellPresenter alloc]initWithModel:model]];
    }
}


-(UICollectionReusableView *)headerViewWithCollectionView:(UICollectionView *)collectionView indexpath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];
    headerView.backgroundColor = [UIColor blueColor];
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(0, 0, 0, 0);
    label.text = @"公版时光书";
    [label sizeToFit];
    label.backgroundColor = [UIColor orangeColor];
    [headerView addSubview: label];
    return headerView;
    
}

- (CGSize)sizeForItemAtIndex:(NSInteger)index{
 
    if (self.cellPArray.count == 0) {
        return CGSizeZero;
    }
    
    BooksCellPresenter *cellP = self.cellPArray[index];
    return CGSizeMake(200, [cellP cellHeight]);
    
}

+ (instancetype)presentWithView:(UICollectionView *)collectionView{
    
    BookSectionPresenter *sp = [[BookSectionPresenter alloc]init];
    sp.collectionView = collectionView;
    return sp;
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath{
    
    BooksCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(self.class) forIndexPath:indexPath];
    cell.p = [self.cellPArray objectAtIndex:indexPath.row];
    [cell.p presenterWithView:cell];
    return cell;
    
}


-(NSMutableArray *)cellPArray{
    if (!_cellPArray) {
        _cellPArray = [NSMutableArray array];
    }
    return _cellPArray;
}
@end
